/**
 * 
 */
package com.gmail.blackdog1987.carcare.activity;

import in.srain.cube.image.CubeImageView;
import in.srain.cube.image.ImageLoader;
import in.srain.cube.image.ImageLoaderFactory;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.gmail.blackdog1987.carcare.App;
import com.gmail.blackdog1987.carcare.BaseActivity;
import com.gmail.blackdog1987.carcare.R;
import com.gmail.blackdog1987.carcare.db.dao.UserDao;
import com.gmail.blackdog1987.carcare.entity.TCarRent;
import com.gmail.blackdog1987.carcare.entity.TMaintainDetail;
import com.gmail.blackdog1987.carcare.entity.TProduct;
import com.gmail.blackdog1987.carcare.network.request.builder.OrderCarRentRequestBuilder;
import com.gmail.blackdog1987.carcare.network.response.IResponse;
import com.gmail.blackdog1987.carcare.network.response.MeirongDetailResponse;
import com.gmail.blackdog1987.carcare.network.response.OrderCarRentResponse;
import com.gmail.blackdog1987.carcare.network.response.OrderMeirongResponse;
import com.gmail.blackdog1987.carcare.network.response.handler.IResponseHandler;
import com.gmail.blackdog1987.carcare.network.response.handler.OnResponseListener;
import com.gmail.blackdog1987.carcare.network.response.handler.OrderCarRentResponseHandler;
import com.gmail.blackdog1987.carcare.util.ModelUtil;
import com.gmail.blackdog1987.carcare.widget.TimePicker;

@EActivity(R.layout.activity_order_carrent)
public class OrderCarRentActivity extends BaseActivity implements OnResponseListener {
	private Context mContext;

	// views
	@ViewById(R.id.order_carrent_gettime)
	public TextView mGetTime;
	@ViewById(R.id.order_carrent_returntime)
	public TextView mReturnTime;
	@ViewById(R.id.order_carrent_price)
	public TextView price;
	@ViewById(R.id.order_carrent_days)
	public TextView days;
	@ViewById(R.id.order_carrent_title)
	public TextView title;
	@ViewById(R.id.order_carrent_name)
	public EditText name;
	@ViewById(R.id.order_carrent_phone)
	public EditText phone;
	@ViewById(R.id.order_carrent_head)
	public CubeImageView mHead;
	TCarRent car;
	Date dateGet;
	Date dateReturn;

	OrderCarRentRequestBuilder mReq = new OrderCarRentRequestBuilder();
	OrderCarRentResponseHandler mHandler = new OrderCarRentResponseHandler();

	@AfterViews
	public void afterViews() {
		mContext = this;
		initData();
		reg();
	}

	public void reg() {
		mHandler.setOnResponseListener(this);
	}

	public void initData() {
		car = (TCarRent) getIntent().getSerializableExtra("data");
		if(car==null){
			Toast.makeText(mContext, "发生未知错误，请刷新", Toast.LENGTH_LONG).show();
			finish();
			return;
		}
		title.setText(ModelUtil.getCarrentTitle(car));
		if(car.getHeadurl()!=null&&car.getHeadurl().length()!=0){
			ImageLoader imageLoader = ImageLoaderFactory.create(mContext);
			App.loadImg(mHead, imageLoader, car.getHeadurl());
		}
	}

	private void setPrice() {
		if (dateGet != null && dateReturn != null) {
			if (dateGet.after(dateReturn)) {
				days.setText("0");
				price.setText("0");
			} else {
				double day = ((dateReturn.getTime() - dateGet.getTime()) / (1000 * 3600 * 24.0));
				long d = Math.round(day);
				days.setText(String.valueOf(d));
				price.setText(String.valueOf(d * (car.getPrice()==null?0:car.getPrice())));
			}
		} else {
			days.setText("0");
			price.setText("0");
		}
	}

	// click area
	@Click(R.id.back)
	void back() {
		finish();
	}

	@Click(R.id.order_carrent_gettime)
	void getTime() {
		Intent startIntent = new Intent(OrderCarRentActivity.this, TimePicker.class);
		startActivityForResult(startIntent, R.id.order_carrent_gettime);
	}

	@Click(R.id.order_carrent_returntime)
	void returnTime() {
		Intent endIntent = new Intent(OrderCarRentActivity.this, TimePicker.class);
		startActivityForResult(endIntent, R.id.order_carrent_returntime);
	}

	@Click(R.id.order_carrent_order)
	void selectCar() {
		if (!App.validUser(this))
			return;
		if(!validInput()){
			return;
		}
		mReq.setCarid(String.valueOf(car.getId())).setName(name.getText().toString()).setPhone(phone.getText().toString()).setDays(days.getText().toString())
				.postJsonRequest(mContext, mHandler);
	}
	
	private boolean validInput() {
		if (mGetTime.getText().toString().length() == 0 || mReturnTime.getText().toString().length() == 0 || Integer.valueOf(days.getText().toString()) == 0) {
			Toast.makeText(mContext, "请填写正确信息", Toast.LENGTH_LONG).show();
			return false;

		}
		return true;
	}

	@Override
	public void onResponse(IResponseHandler classz) {
		TProduct product = new TProduct();
		product.body = "汽车租赁";
		product.price = ((OrderCarRentResponse) classz.getResponse()).order.totalprice;
		product.subject = "车拉丁";
		product.tableName = "t_order_carrent";
		product.tableId = ((OrderCarRentResponse) classz.getResponse()).order.getId();
		product.userid = new UserDao().getUser().id;
		Intent i = new Intent(OrderCarRentActivity.this, PayActivity.class);
		i.putExtra("data", product);
		i.putExtra("url", car.headurl);
		startActivityForResult(i, PayActivity.REQUESTCODE_PAY);
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == Activity.RESULT_OK) {
			switch (requestCode) {
			case PayActivity.REQUESTCODE_PAY:
				Toast.makeText(mContext, "恭喜您支付成功", Toast.LENGTH_LONG).show();
				finish();
				break;
			case R.id.order_carrent_gettime:
				long time = data.getLongExtra("time", -1);
				if (time != -1) {
					dateGet = new Date(time);
					DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
					String tmp = format.format(dateGet);
					mGetTime.setText(tmp);
					setPrice();
				}
				break;
			case R.id.order_carrent_returntime:
				long time2 = data.getLongExtra("time", -1);
				if (time2 != -1) {
					dateReturn = new Date(time2);
					DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
					String tmp2 = format.format(dateReturn);
					mReturnTime.setText(tmp2);
					setPrice();
				}
				break;
			}
		}
	}

	@Override
	public void onError(IResponseHandler classz) {
		Toast.makeText(mContext, "下单失败", Toast.LENGTH_LONG).show();
	}
}
